package cn.lds.health.service.impl;

import cn.lds.health.constant.DsHealthConstant;
import cn.lds.health.dto.AppointMentRecordCondition;
import cn.lds.health.dto.Pager;
import cn.lds.health.dto.ResultDTO;
import cn.lds.health.entity.AppointmentRecord;
import cn.lds.health.exception.SysException;
import cn.lds.health.mapper.AppointmentRecordMapper;
import cn.lds.health.service.AppointmentRecordService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AppointmentRecordService
 * @Description TODO
 * @Author Christopher
 * @Date 2023/8/9 10:08
 * @Version 1.0
 **/

@Service
public class AppointmentRecordServiceImpl implements AppointmentRecordService {
	
	@Autowired
	private AppointmentRecordMapper appointmentRecordMapper;
	
	@Override
	public ResultDTO<List<AppointmentRecord>> selectAppointMentRecordByCondition(AppointMentRecordCondition condition, Pager pager) throws SysException {
		
		//根据条件查询数据行数
		int count = appointmentRecordMapper.selectAppointCount(condition);
		
		//设置行数，生成总页书
		pager.setTotalRow(count);
		
		//查询数据
		List<AppointmentRecord> appointmentRecords = appointmentRecordMapper.selectAppointMentRecordByCondition(condition, pager);
		
		if (CollectionUtils.isEmpty(appointmentRecords)) {
			throw new SysException(DsHealthConstant.Query.QUERY_ERROR);
		}
		
		return ResultDTO.buildSuccess(appointmentRecords);
	}
	
	@Override
	public ResultDTO addAppointment(AppointmentRecord record) throws SysException {
		//定义创建数据创建时间
		record.setCreateTime(new Date());
		
		int i = appointmentRecordMapper.addAppointment(record);
		
		if (i <= 0){
			throw new SysException(DsHealthConstant.Insert.ADD_ERROR);
		}
		
		return ResultDTO.buildSuccess();
	}
	
	@Override
	public ResultDTO confirmAppoint(Integer id) throws SysException {
		int i = appointmentRecordMapper.confirmAppoint(id);
		
		if (i < 0){
			throw new SysException(DsHealthConstant.Insert.ADD_ERROR);
		}
		
		return ResultDTO.buildSuccess();
	}
	
	@Override
	public ResultDTO cancelAppoint(Integer id) throws SysException {
		
		int i = appointmentRecordMapper.cancelAppoint(id);
		
		if (i < 0){
			throw new SysException(DsHealthConstant.Insert.ADD_ERROR);
		}
		
		return ResultDTO.buildSuccess();
	}
}